import axios from "axios";
import {
  ElLoading,
  ElMessage
} from 'element-plus'
let isDev = process.env.NODE_ENV === "development";
// isDev = false;
let baseURL;
// if (isDev) {
//   baseURL = "http://localhost:3007";
// } else {
//   baseURL = "http://huruqing.cn:3007";
// }

const service = axios.create({
  baseURL:"http://localhost:3007",
  timeout: 30000, // 请求超时时间(因为需要调试后台,所以设置得比较大)
});

let loadObj;
// request拦截器
service.interceptors.request.use(
  (config) => {
    if (!loadObj) {
      loadObj = ElLoading.service({
        lock: true,
        text: 'Loading',
        background: 'rgba(0, 0, 0, 0.7)',
      })
    }
    return config;
  },
  (error) => {
    setTimeout(() => {
      loadObj.close();
    }, 300);
    return Promise.reject(error);
  }
);

// response 拦截器
service.interceptors.response.use(
  (response) => {
    setTimeout(() => {
      loadObj.close();
    }, 300);
    const res = response.data;
    if (res.code == 666) {
      return res;
    } else {
      ElMessage(res.msg);
      return Promise.reject(res.msg);
    }
  },
  (error) => {
    setTimeout(() => {
      loadObj.close();
    }, 300);
    ElMessage(error.message);
    return Promise.reject(error.message);
  }
); 
export default service;